package com.offcn.member.controller;

import com.offcn.common.utils.JWTUtil;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;
import com.offcn.member.entity.MemberEntity;
import com.offcn.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 会员-会员表
 *
 * @author offcn
 * @email hk109@126.com
 * @date 2023-03-06 14:13:18
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //注入redis操作工具对象
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //读取指定时间范围内注册用户统计数据
    @RequestMapping("countAccountCreate")
    public R countAccountCreate(String beginTime, String endTime){
        List<Map<String, Object>> mapList = memberService.countByDateTime(beginTime, endTime);
        return R.ok().put("mapList",mapList);
    }

    //登录方法
    @PostMapping("login")
    public R login(String username, String password){
        MemberEntity memberEntity = memberService.login(username, password);
        //判断用户对象是否为空
        if(memberEntity==null){
            return R.error("登录失败");
        }

        //用户登录成功，生成jwt令牌
        String token = JWTUtil.generateToken(username);

        //使用uuid生成一个字符串  wwwwe-dfffff-errrr-ddddd
        String refreshToken = UUID.randomUUID().toString().replace("-", "");

        //把生成token令牌存储到redis
        redisTemplate.boundHashOps(refreshToken).put("token",token);
        //把username存储到redis
        redisTemplate.boundHashOps(refreshToken).put("username",username);

        //给令牌设置一个有效期
        redisTemplate.expire(refreshToken, JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);

        return R.ok("登录成功").put("refreshToken",refreshToken);
    }

    //使用刷新令牌更换新令牌
    @PostMapping("refreshToken")
    public R refreshToken(String refreshToken){
        //根据refreshToken去redis获取该令牌所属用户名
       String username= (String) redisTemplate.boundHashOps(refreshToken).get("username");
       //判断用户名如果为空
        if(StringUtils.isEmpty(username)){
            return R.error("刷新令牌不存在");
        }
        //根据username重新生成一个token
        String token = JWTUtil.generateToken(username);
        //更新保存新的令牌到redis
        redisTemplate.boundHashOps(refreshToken).put("token",token);
        //更新令牌有效期
        redisTemplate.expire(refreshToken, JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
        return R.ok("刷新令牌成功").put("token",token).put("refreshToken",refreshToken);
    }

}
